package dao;

import conexao.Conexao;
import static conexao.Conexao.getConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

/**
 *
 * @author WELISSON
 */
public class GenericDAO {

    protected Connection conn;

    public GenericDAO() {
        this.conn = Conexao.getConnection();
    }

    public Connection getConn() {
        return conn;
    }

    public void save(String insertSql, Object... parametros) throws SQLException {
        PreparedStatement pstmt
                = getConnection().prepareStatement(insertSql);

        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i] instanceof Date) {
                Date dt = (Date) parametros[i];
                pstmt.setDate(i + 1, new java.sql.Date(dt.getTime()));
            } else {
                pstmt.setObject(i + 1, parametros[i]);
            }
        }
        
        pstmt.execute();

        pstmt.close();
        conn.close();
        System.out.println(conn.isClosed());
    }

    public int saveReturnId(String insertSql, Object... parametros) throws SQLException {
        int id = 0;
        PreparedStatement pstmt;

        pstmt = getConnection().prepareStatement(insertSql, PreparedStatement.RETURN_GENERATED_KEYS);
        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i] instanceof Date) {
                Date dt = (Date) parametros[i];
                pstmt.setDate(i + 1, new java.sql.Date(dt.getTime()));
            } else {
                pstmt.setObject(i + 1, parametros[i]);
            }
        }

        pstmt.executeUpdate();
        ResultSet rs = pstmt.getGeneratedKeys();
        while (rs.next()) {
            id = rs.getInt(1);
            System.out.println(conn.isClosed());
        }

        pstmt.close();
        conn.close();
        

        return id;
    }
    
    public String saveReturnIdString(String insertSql, Object... parametros) throws SQLException {
        String id = "";
        PreparedStatement pstmt;

        pstmt = getConnection().prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);
        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i] instanceof Date) {
                Date dt = (Date) parametros[i];
                pstmt.setDate(i + 1, new java.sql.Date(dt.getTime()));
            } else {
                pstmt.setObject(i + 1, parametros[i]);
            }
        }

        pstmt.executeUpdate();
        ResultSet rs = pstmt.getGeneratedKeys();
        while (rs.next()) {
            id = rs.getString(1);
        }

        pstmt.close();
        conn.close();
        System.out.println(conn.isClosed());

        return id;
    }

    public void update(String updateSql, Object id, Object... parametros) throws SQLException {
        PreparedStatement pstmt
                = getConnection().prepareStatement(updateSql);

        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i] instanceof Date) {
                Date dt = (Date) parametros[i];
                pstmt.setDate(i + 1, new java.sql.Date(dt.getTime()));
            } else {
                pstmt.setObject(i + 1, parametros[i]);
            }
        }
        pstmt.setObject(parametros.length + 1, id);
        pstmt.execute();
        System.out.println("close!");
        pstmt.close();
        conn.close();
        System.out.println(conn.isClosed());
    }

    public void delete(String deleteSql, Object... parametros) throws SQLException {
        PreparedStatement pstmt
                = getConnection().prepareStatement(deleteSql);

        for (int i = 0; i < parametros.length; i++) {
            pstmt.setObject(i + 1, parametros[i]);
        }

        pstmt.execute();
        
        pstmt.close();
        conn.close();
        System.out.println(conn.isClosed());
    }

}
